package com.lgx.mobile.rest.user;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lgx.common.exception.DataException;
import com.lgx.right.entity.User;
import com.lgx.right.service.IUserService;

/**
 * 
 * @author xuyichen
 *
 */
@Controller
@RequestMapping("/modifyPassword")
public class ModifyPasswordController {
	
	private static final Logger logger = Logger.getLogger(ModifyPasswordController.class);

	@Autowired
	IUserService userService;
	
	public IUserService getUserService() {
		return userService;
	}
	
	/**
	 * 密码修改
	 * @param userId id is key
	 * @param password p is key
	 * @param newPassword np is key
	 * @return 0：原始密码错误；1：成功；2：失败；3：参数异常，有参数为空
	 */
	@RequestMapping(value="/{id}/{p}/{np}",method=RequestMethod.GET)
	@ResponseBody
	public Object modifyPassword(@PathVariable("id") String userId,@PathVariable("p") String password,@PathVariable("np") String newPassword){
		if(StringUtils.isNotBlank(userId)&&StringUtils.isNotBlank(password)&&StringUtils.isNotBlank(newPassword)){
			User user = getUserService().findUserById(Long.parseLong(userId));
			if(user!=null){
				String status = "2";
				try {
					status = getUserService().modifyPassword(Long.parseLong(userId), newPassword, password);
				} catch (NumberFormatException e) {
					e.printStackTrace();
				} catch (DataException e) {
					e.printStackTrace();
				}
				return status;
			}else{
				if(logger.isDebugEnabled()){
					logger.debug("modifyPassword faild userId or newPassword is error");
				}
//				"modifyPassword faild please check userId is exists";
				return "3";
			}
		}else{
			if(logger.isDebugEnabled()){
				logger.debug("modifyPassword faild userId or newPassword is not filled");
			}
//		    "modifyPassword faild please check userId or newPassword is filled";
			return "4";
		}
	}
}
